package de.wittje.games.mlpbd.view.panels;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;
import javax.swing.JPanel;

import de.wittje.games.mlpbd.controller.ExternalizingController;
import de.wittje.games.mlpbd.controller.SoundController;
import de.wittje.games.mlpbd.view.windows.MainWindow;

public class TitlePanel extends JPanel {

	private ExternalizingController ec;
	private final String BACKGROUNDPFAD = "/de/wittje/games/mlpbd/data/images/titleBackground.png";
	private final String LOGOPFAD = "/de/wittje/games/mlpbd/data/images/logo.png";
	private final String SUBTITLEPFAD = "/de/wittje/games/mlpbd/data/images/subtitle";
	private final String CURSORPFAD = "/de/wittje/games/mlpbd/data/images/cursor.png";

	private int cursorPos = 1;
	private int textWidth, textHeight;
	private SoundController sc;
	private MainWindow window;

	public TitlePanel(ExternalizingController ec, MainWindow window) {

		this.ec = ec;
		sc = new SoundController();
		this.window = window;

	}

	@Override
	public void paint(Graphics g) {
		super.paint(g);

		setBackground(new Color(240, 177, 213));

		drawBackground(g);
		drawLogo(g);
		drawMenuUnderlay(g);
		drawMenu(g);
		drawCursor(g);

	}

	private void drawMenuUnderlay(Graphics g) {

		String newGameText = ec.getResource("newGame");
		String continueText = ec.getResource("continue");
		String optionText = ec.getResource("option");
		String exitText = ec.getResource("exit");

		InputStream is = this.getClass().getResourceAsStream(
				"/de/wittje/games/mlpbd/data/Equestria.ttf");
		Font f;
		try {
			f = Font.createFont(Font.TRUETYPE_FONT, is);
			f = f.deriveFont(50f);
			g.setFont(f);

			FontMetrics fm = g.getFontMetrics();

			int menuX = 0;
			int menuY = 0;

			int y = (getHeight() + fm.getHeight() * 1) / 2 + getHeight() / 4
					+ 5 * 1;
			int yMax = (getHeight() + fm.getHeight() * 4) / 2 + getHeight() / 4
					+ 5 * 4;
			int x = 0;

			if (fm.stringWidth(newGameText) > textWidth) {
				textWidth = fm.stringWidth(exitText);
				x = (getWidth() - fm.stringWidth(newGameText)) / 2;
			}
			if (fm.stringWidth(exitText) > textWidth) {
				textWidth = fm.stringWidth(exitText);
				x = (getWidth() - fm.stringWidth(exitText)) / 2;
			}

			if (fm.stringWidth(optionText) > textWidth) {
				textWidth = fm.stringWidth(optionText);
				x = (getWidth() - fm.stringWidth(optionText)) / 2;
			}

			if (fm.stringWidth(continueText) > textWidth) {
				textWidth = fm.stringWidth(continueText);
				x = (getWidth() - fm.stringWidth(continueText)) / 2;
			}

			menuY = y - fm.getHeight() + 15;
			menuX = x - 20;
			g.setColor(new Color(0, 0, 0, 128));
			g.fillRect(menuX, menuY, textWidth + 40, yMax - menuY + 15);
		} catch (FontFormatException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	private void drawCursor(Graphics g) {

		BufferedImage img;
		try {
			img = ImageIO.read(this.getClass().getResourceAsStream(CURSORPFAD));
			int x = (getWidth() - textWidth) / 2 - img.getWidth() - 10;
			int y = (getHeight() + textHeight * cursorPos) / 2 + getHeight()
					/ 4 + 5 * cursorPos - textHeight / 2 - img.getHeight() / 2
					+ 10;

			g.drawImage(img, x, y, null, null);
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	private void drawBackground(Graphics g) {

		BufferedImage img;
		try {
			img = ImageIO.read(this.getClass().getResourceAsStream(
					BACKGROUNDPFAD));
			int x = (getWidth() - img.getWidth()) / 2;
			int y = getHeight() - img.getHeight();
			g.drawImage(img, x, y, null, null);
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	private void drawLogo(Graphics g) {

		try {
			BufferedImage img = ImageIO.read(this.getClass()
					.getResourceAsStream(LOGOPFAD));
			int x = 0 - (640 - getWidth()) / 2;
			int y = 0;
			if (x < 0) {
				x = 0;
			}
			if (y < 0) {
				y = 0;
			}
			g.drawImage(img, x, y, null, null);

			img = ImageIO.read(this.getClass().getResourceAsStream(
					SUBTITLEPFAD + "_" + ec.getLocale() + ".png"));

			x = 0 - (400 - getWidth()) / 2;
			y = 170;

			if (x < 120) {
				x = 120;
			}

			g.drawImage(img, x, y, null, null);

		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	private void drawMenu(Graphics g) {

		try {

			String newGameText = ec.getResource("newGame");
			String continueText = ec.getResource("continue");
			String optionText = ec.getResource("option");
			String exitText = ec.getResource("exit");

			InputStream is = this.getClass().getResourceAsStream(
					"/de/wittje/games/mlpbd/data/Equestria.ttf");
			Font f = Font.createFont(Font.TRUETYPE_FONT, is);
			f = f.deriveFont(50f);
			g.setFont(f);

			FontMetrics fm = g.getFontMetrics();

			textHeight = fm.getHeight();
			int x = (getWidth() - fm.stringWidth(newGameText)) / 2;
			int y = (getHeight() + fm.getHeight() * 1) / 2 + getHeight() / 4
					+ 5 * 1;

			g.setColor(new Color(255, 153, 27));

			g.drawString(newGameText, x, y);

			x = (getWidth() - fm.stringWidth(continueText)) / 2;
			y = (getHeight() + fm.getHeight() * 2) / 2 + getHeight() / 4 + 5
					* 2;

			g.drawString(continueText, x, y);

			x = (getWidth() - fm.stringWidth(optionText)) / 2;
			y = (getHeight() + fm.getHeight() * 3) / 2 + getHeight() / 4 + 5
					* 3;

			g.drawString(optionText, x, y);

			x = (getWidth() - fm.stringWidth(exitText)) / 2;
			y = (getHeight() + fm.getHeight() * 4) / 2 + getHeight() / 4 + 5
					* 4;

			g.drawString(exitText, x, y);

		} catch (FontFormatException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public void moveCursorDown() {
		if (cursorPos < 4) {
			cursorPos++;
			sc.playSound("selectionchange");
		}
	}

	public void moveCursorUp() {
		if (cursorPos > 1) {
			cursorPos--;
			sc.playSound("selectionchange");
		}
	}

	public void select() {
		sc.playSound("select");

		switch (cursorPos) {
		case 1:
			// TODO new Game
			break;
		case 2:
			// TODO continue
			break;
		case 3:
			window.setContent("option");
			break;
		case 4:
			System.exit(0);
			break;
		}

	}
}
